<?php
namespace app\admin\controller;

use think\Controller;
use think\Session;
use think\Request;

class Login extends Controller
{
    /**
     * 登录页面
     */
    public function login(Request $request)
    {
        if(Session::get('user'))
        {
            $this->redirect('index/index');
        }
        if ($request->isPost())
        {
            if($data = model('adminUsers')->checkLogin($request->post()))
            {
                Session::set('user',array(
                    'id' => $data['id'],
                    'username' => $data['username'],
                    'avatar' => $data['avatar']
                ));
                $return = ['status' => 1, 'msg' => '登录成功、前往管理后台'];
            } else {
                $return = ['status' => 0, 'msg' => '账号或密码错误'];
            }
            return $return;
        } else {
            return $this->fetch();
        }
    }

    /**
     * 退出登录
     */
    public function logout(){
        Session::clear();
        $this->redirect('login/login');
    }
}
